$(function () {
    //历史保单信息表格
    var historyBill_datagrid = $("#historyBill_datagrid");
    //查看保单 已选明细
    var showproductInfo_datagrid = $("#showproductInfo_datagrid");
    var show_dialog = $("#show_dialog");
    var carinfos = null;
    //获取所有汽车对象
    $.post('/car/query',function(data){
        carinfos = data
    });
    /** =====================================历史保单信息表格===================================== */
    historyBill_datagrid.datagrid({
        url: "/historyBill/query",
        fitColumns: true,
        fit: true,
        striped: true,
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true,
        toolbar: "#historyBill_toolbar",
        columns: [[
            {field: 'id', checkbox: true},
            {field: 'number', title: '保险单号', width: 80},
            {field: 'policyClient', title: '投保人', width: 80, formatter: function (value, row) {
                    if (row.policyClient) {
                        return row.policyClient.name;
                    }
                }},
            {field: 'car', title: '车牌号', width: 80,formatter:function (value, row) {
                    if (value) {
                        return value.number;
                    }
                }},
            {field: 'vdate', title: '申请日期', width: 80},
            {field: 'auditDate', title: '核保日期', width: 80},
            {field: 'beginDate', title: '起保日期', width: 80},
            {field: 'endDate', title: '止保日期', width: 80},
            {field: 'limitYear', title: '保险时长', width: 80},
            {field: 'opEmployee', title: '业务员', width: 80, formatter: function (value, row) {
                    if (value) {return value.username;}
                }},
            {field: 'auditor', title: '审核人', width: 80, formatter: function (value, row) {
                if (value) {return value.username;}
            }},
            {field: 'totalPrice', title: '投保总金额', width: 80},
            {field: 'state', title: '状态', width: 80,formatter:function (value, row) {
                    if(value == 1){
                        return "<font color='red'>暂存单</font>";
                    }
                    if(value == 2){
                        return "<font color='red'>退回修改</font>";
                    }
                    if(value == 3){
                        return "<font color='red'>待审核</font>";
                    }
                    if(value == 4){
                        return "<font color='red'>已审核未缴费</font>";
                    }
                    if(value == 5){
                        return "<font color='green'>已缴费</font>";
                    }
                    if(value == 6){
                        return "<font color='red'>拒保</font>";
                    }
            }}
        ]]
    });
    /** ==================================================================================== */

    /** ====================================保单查看弹出框=================================== */
    $("#showBill").click(function () {
        //编辑方法
        var row = historyBill_datagrid.datagrid("getSelected");
        if (!row) {
            $.messager.alert("温馨提示", '至少选择一条数据', 'warning')
            return;
        }
        //传递车辆信息,将car的id传递过去查询当前车辆的信息
        $.get('/car/selectCarByBillId', {id: row.car.id}, function (data){
            //维护属性carSizeDetail.id
            //数据字典的时候使用data["carSizeDetail.id"] = data.carSizeDetail.id;
            $("#showCarinfo_form").form('load',data);
        });
        //传递产品信息,将当前行的保单编码传递到后台通过保单编号查询当前产品信息,当前行是guaranteeBill的对象
        $.get("/product/selectProductList",{number:row.number},function (data) {
            if (data) {
                //产品机构id 回显
                $("#showAgencyInfo_combobox").combobox('setValue',data[0].company.id);
                //产品明细数据加载
                showproductInfo_datagrid.datagrid('loadData',data);
            }
            //保险年限 从暂存单当中获取row.limitYear
            $("#showlimitYear_input").textbox('setValue',row.limitYear);
            //总价显示
            $("#showtotalAmount1").html(row.totalPrice);
        });
        show_dialog.dialog('open');
        show_dialog.dialog('center');

    });
    //初始化弹窗
    $("#show_dialog").dialog({
        modal: true,
        closed: true,
        title: '查看保单信息',
        width: 700,
        height: 470,
        buttons: '#showInfo_buttons',
        top: 200
    });
    //保单详情查看
    showproductInfo_datagrid.datagrid({
        url: '/product/selectProductList',
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '产品名称', width: 100},
            {field: 'annualFee', title: '保险年费', width: 100},
            {field: 'year', title: '年限', width: 100, align: 'right'}
        ]]
    });
    //点击关闭事件
    $("#showInfo_cancel").click(function () {
        $("#show_dialog").dialog("close");
    });
    //刷新表格
    $("#refresh").click(function () {
       historyBill_datagrid.datagrid('reload');
    });
    //多条件查询
    $("#query").click(function () {
        //获得当前输入文本框的值
        var clientName = $("#clientName").textbox('getValue')
        var sn = $("#sn").textbox('getValue')
        //加载的时候把参数传递过去
        historyBill_datagrid.datagrid('load', {
            clientName: clientName,
            sn: sn
        });
    })
    /** ==================================================================================== */
});